/*
 * 文件名称: AdminService.java
 * 版权信息: Copyright 2005-2013 SKY-MOBI Inc. All right reserved.
 * ----------------------------------------------------------------------------------------------
 * 修改历史:
 * ----------------------------------------------------------------------------------------------
 * 修改原因: 新增
 * 修改人员: Allen.Hu
 * 修改日期: 2013-3-30
 * 修改内容: 
 */
package com.drzui.tickets.admin.service;

import java.util.List;
import org.bigmouth.framework.dao.generator.Service;
import org.bigmouth.framework.util.MD5Utils;
import org.bigmouth.framework.util.result.Result;
import com.drzui.tickets.admin.AdminSessionUtils;
import com.drzui.tickets.admin.dao.AdminDao;
import com.drzui.tickets.admin.entity.Admin;
import com.drzui.tickets.admin.entity.AdminCondition;
import com.drzui.tickets.commons.ErrorCodeUtils;
import com.drzui.tickets.commons.exception.ParameterException;

public class AdminService extends
		Service<Admin, AdminCondition, Integer, AdminDao> {

	private AdminDao adminDao;

	public boolean login(Admin admin) {

		this.validateLogin(admin);

		AdminCondition condition = new AdminCondition();
		String encodePassword = MD5Utils.encode(admin.getPassword());
		
		condition.createCriteria().andLoginnameEqualTo(admin.getLoginname())
				.andPasswordEqualTo(encodePassword);
		
		List<Admin> rs = adminDao.selectByCondition(condition);
		if (rs != null && rs.size() > 0) {
			// Login success.
			AdminSessionUtils.putSession(rs.get(0));
			return true;
		}
		return false;
	}

	/**
	 * 登录验证
	 * 
	 * @param user 登录信息
	 */
	private void validateLogin(Admin admin) {
		if (admin == null) {
			throw new ParameterException(
					ErrorCodeUtils.NotFoundParameter("登录信息"));
		}
		Result result = null;
		result = admin.loginValidate();
		if (!result.isSuccess()) {
			throw new ParameterException(result.getErrorMessage());
		}
	}

	@Override
	protected AdminDao getDao() {
		return adminDao;
	}

	public AdminDao getAdminDao() {
		return adminDao;
	}

	public void setAdminDao(AdminDao adminDao) {
		this.adminDao = adminDao;
	}
}
